package com.m1.tzolkin.model.util.board;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.m1.tzolkin.model.exceptions.EndOfGameException;
import com.m1.tzolkin.model.exceptions.NoMovedWorkersException;
import com.m1.tzolkin.model.exceptions.WaitingOperationsException;
import com.m1.tzolkin.model.util.CycleList;
import com.m1.tzolkin.model.util.ICycleList;
import com.m1.tzolkin.model.util.player.IPlayer;


public class Board<E extends IPlayer> implements IBoard<E> {
	
	// ATTRIBUTS
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2935804097535588479L;
	private final ICycleList<E> playersManager;
	
	// CONSTRUCTEUR
	
	public Board(Collection<E> players) {
		if (players == null || players.size() < 2) {
			throw new IllegalArgumentException();
		}
		playersManager = new CycleList<E>(players);
	}
	
	// REQUETES

	@Override
	public E getCurrentPlayer() {
		return playersManager.getCurrent();
	}

	@Override
	public E getFirstPlayer() {
		return playersManager.getFirst();
	}

	@Override
	public List<E> getPlayers() {
		return new ArrayList<E>(playersManager);
	}
	
	/**
	 * Retourne le manager de joueurs de ce plateau.
	 */
	protected ICycleList<E> getPlayersManager() {
		return playersManager;
	}
	
	// COMMANDES

	@Override
	public E endTurn() throws WaitingOperationsException, EndOfGameException, NoMovedWorkersException {
		playersManager.incCurrent();
		return playersManager.getCurrent();
	}

}
